#!/bin/bash
#
# This will be executed by a script on /etc/cron.houly/Create-TheSource-Index-Links
#     That file will contain the following two lines 
#                    #!/bin/bash
#                    /var/www/html/WebsiteContentMenu/Web_Internals/PHPScripts/TableMenu/Cron_Create_Index_Link | logger
#
#     ___   ____ 
#    / _ \ / ___)
#   | |_| | |    
#    \___/|_|    
#             
#
# This can be added to root's crontab with the command crontab -e  as 
#
#             #min hour  dom mon dow   command
#
#             #Every 30 minutes
#             ###*/30 * * * * /var/www/html/Utils/TableMenu/Cron-Create-Index-Link | logger
#
#             #Every hour
#             ###0 * * * * /var/www/html/Utils/TableMenu/Cron-Create-Index-Link | logger
#             ###@hourly      /var/www/html/Utils/TableMenu/Cron-Create-Index-Link | logger
#             #========================================================================
#
#             # * * * * *    Command
#             # | | | | |      |
#             # | | | | |      +--   command to execute
#             # | | | | |
#             # | | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
#             # | | | +------ Month                     (range: 1-12)
#             # | | +-------- Day of the Month  (range: 1-31)
#             # | +---------- Hour                         (range: 0-23)
#             # +------------ Minute                     (range: 0-59)
#
#             #min hour  dom mon dow   command
#
#             #Instead of the first five fields, one of eight special strings may appear:
#
#              #string         meaning
#              #------         -------
#              @reboot        Run once, at startup.
#              @yearly        Run once a year, "0 0 1 1 *".
#              @annually      (same as @yearly)
#              @monthly       Run once a month, "0 0 1 * *".
#              @weekly        Run once a week, "0 0 * * 0".
#              @daily         Run once a day, "0 0 * * *".
#              @midnight      (same as @daily)
#              @hourly        Run once an hour, "0 * * * *".
#


echo "* * * Begin Cron-Create-Index-Link"

# Set the file that will be linked to
TargetLink="/var/www/html/WebsiteContentMenu/Web_Internals/PHPScripts/TableMenu/index.php"

if [ ! -e "${TargetLink}" ];  then
    echo "   error: The file you wish to create a link to does not exist"
    exit 1
fi

echo "      - If a link is not found, one will be created  linking index.php to $TargetLink"

 
tree_contains_INDEX_file()
{
    # return true (0) as soon as we find a "index.php" file

    find "$1"   -maxdepth 1 -name "index.php" -print0 | read -r -d $'\0' file && return 0

    return 1
}




#
# Do the  directory/var/www/html/WebsiteContentMenu/Dirs     directory  =======================================================================
#

TargetDir="/var/www/html/WebsiteContentMenu/Dirs"
if [ ! -d "${TargetDir}" ];  then
    echo "   error: The target directory that you supplied $TargetDir does not exist"
    exit 1
fi
echo "        o Recursively looking for missing index.php links in $TargetDir/ ..."

find "$TargetDir" -depth -type d -print0 | while read -r -d $'\0' dir; do
     if ! tree_contains_INDEX_file "$dir"; then
        LinkName="$dir"/index.php
        echo "          + Adding Symbolic Link via command      ln -rsf  $TargetLink  $LinkName"
        ln -rsf  "$TargetLink"  "$LinkName"
     fi
done


#
# Do the /var/www/html/WebsiteContentMenu/Files     directory   =======================================================================
#
TargetDir="/var/www/html/WebsiteContentMenu/Files"
if [ ! -d "${TargetDir}" ];  then
    echo "   error: The target directory that you supplied $TargetDir does not exist"
    exit 1
fi
echo "        o Recursively looking for missing index.php links in $TargetDir/ ..."

find "$TargetDir" -depth -type d -print0 |  while read -r -d $'\0' dir; do
     if ! tree_contains_INDEX_file "$dir"; then
        LinkName="$dir"/index.php
        if [ "$dir" == "/var/www/html/WebsiteContentMenu/Files/Blah Blah Blah" ]; then 
           echo "         ............................................................. Skip this dir $dir"
           Nothing=""
        else
           echo "          + Adding Symbolic Link via command      ln -rsf  $TargetLink  $LinkName"
           ln -rsf  "$TargetLink"  "$LinkName"
       fi
     fi
done


#
# Do the  directory /var/www/html/WebsiteContentMenu/Files N Dirs     directory  =======================================================================
#

TargetDir="/var/www/html/WebsiteContentMenu/Files N Dirs"
if [ ! -d "${TargetDir}" ];  then
    echo "   error: The target directory that you supplied $TargetDir does not exist"
    exit 1
fi
echo "        o Recursively looking for missing index.php links in $TargetDir/ ..."

find "$TargetDir" -depth -type d -print0 | while read -r -d $'\0' dir; do
     if ! tree_contains_INDEX_file "$dir"; then
        LinkName="$dir"/index.php
        echo "          + Adding Symbolic Link via command      ln -rsf  $TargetLink  $LinkName"
        ln -rsf  "$TargetLink"  "$LinkName"
     fi
done


echo  "* * * End  Cron-Create-Index-Link"
